<template>
  <el-container class="el-consize">
<!--    顶部·-->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商管理系统后台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
<!--      侧边栏-->
<!--      绑定宽 如果值是true则宽为64 如果值为false则宽200px-->
      <el-aside :width="isCollapse ? '64px':'200px'">
<!--        菜单-->
        <div class="toggle-button" @click="toggleColl">|||</div>
        <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#3D9EFF"
            :unique-opened="true"
            :collapse = "isCollapse"
            :collapse-transition="false"
            router
            :default-active="activetion"
        >
<!--          unique-opened必须要绑定才能算属性 不然就是字符串-->
<!--          一级菜单-->
          <!--:index绑定唯一动态数据 如果一级菜单每个index相同那么点击事件时会一起执行 index只接受字符串的值-->
          <el-submenu :index="item.id+''" v-for="item in meanuList" :key="item.id">
            <template slot="title">
              <i :class="menuImgObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
<!--            二级菜单-->
            <!--default-active当前激活菜单index-->
            <el-menu-item :index="'/'+chilItem.path" v-for="chilItem in item.children" :key="chilItem.id" @click="activeMenu('/'+chilItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{chilItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
<!--      内容-->
      <el-main><router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
name: "Home",
  data() {
    return {
      //左侧列表的数组
      meanuList: [],
      //左侧图片对象
      menuImgObj:{
        "125":"iconfont icon-user",
        "103":"iconfont icon-lock_fill",
        "101":"iconfont icon-shangpin",
        "102":"iconfont icon-danju",
        "145":"iconfont icon-baobiao"
      },
    //  是否折叠
      isCollapse:false,
      //激活高亮
      activetion:''
    }
  },
  created() {
    this.getMeanuList();
    this.activetion = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      //清除token
      window.sessionStorage.clear();
      //跳转到登陆页
      this.$router.push('/login')
    },
    //左侧菜单栏的权限
    async getMeanuList(){
      //通过http 传入请求路径 解构赋值给res
      const {data:res} = await this.$http.get('menus');
      if (res.meta.status!==200) return this.$message.error(this.meta.msg);
      this.meanuList = res.data;
    },
//    左侧菜单栏是否折叠
    toggleColl(){
      this.isCollapse = !this.isCollapse;
    },
//    激活菜单栏 高亮
//    如果直接赋值刷新就不会高亮 所以可以存储进token中 刷新之后依然高亮
    activeMenu(activePath){
      window.sessionStorage.setItem("activePath",activePath);
      this.activetion = activePath;
    }
},
}
</script>

<style scoped lang="less">
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  color: white;
  padding:0px;
  font-size: 20px;
  align-items: center;
  div{
    align-items: center;
    display: flex;
    span{
      margin-left: 15px;
    }
  }
}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #eaedf1;
}
.el-consize{
  height: 100%;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: #4a5064;
  color: #fff;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>